<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="反馈id" prop="intro">
        <el-input v-model="dataForm.id" placeholder="反馈id"></el-input>
      </el-form-item>
    <el-form-item label="反馈内容" prop="intro">
      <el-input v-model="dataForm.intro" placeholder="反馈"></el-input>
    </el-form-item>
    <el-form-item label="反馈人" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="反馈人"></el-input>
    </el-form-item>
    <el-form-item label="反馈人姓名" prop="username">
      <el-input v-model="dataForm.username" placeholder="反馈人姓名"></el-input>
    </el-form-item>
    <el-form-item label="反馈时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="反馈时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/teacher/feedback'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          id: 0,
          intro: '',
          userId: '',
          username: '',
          createTime: ''
        },
        dataRule: {
          intro: [
            { required: true, message: '反馈不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '反馈人不能为空', trigger: 'blur' }
          ],
          username: [
            { required: true, message: '反馈人姓名不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '反馈时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0;
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.id) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      }
    }
  }
</script>
